﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.Net;

namespace GotLyrics.LyricGrabbers
{
    public class WowLyricsGrabber : LyricGrabber
    {
        public override WebClient WebClient
        {
            get
            {
                return new WebClient();
            }
        }

        /// <summary>
        /// The website of this lyrics provider. This is pointing to the main page on the website.
        /// </summary>
        /// <value></value>
        public override string WebSiteUrl
        {
            get { return "http://www.wowlyrics.com/"; }
        }

        private string FormatRequestAddress(LyricGrabberContext context)
        {
            string artist = context.Tags.Artist;

            return string.Format("http://www.wowlyrics.com/search2.php?metode=artist&search={0}&cmd=and",
                                 HttpUtility.UrlEncode(artist));
        }

        private string ParseResponse(LyricGrabberContext context, string response)
        {
            string pattern = string.Format("<a\\ href=\"(?<href>.+)\"><b>{0}</b></a>", context.Tags.Artist);

            Match m = Regex.Match(response, pattern, RegexOptions.IgnoreCase);

            if (m.Success)
                response = WebClient.DownloadString(string.Format("{0}{1}", WebSiteUrl, m.Groups["href"].Value));
            
            if (!string.IsNullOrEmpty(response))
            {
                pattern = string.Format("<a\\ href=\"(?<href>.+)\"><b>{0}</b></a>", context.Tags.Title);

                m = Regex.Match(response, pattern, RegexOptions.IgnoreCase);

                if (m.Success)
                {
                    string url = string.Format("{0}{1}", WebSiteUrl, m.Groups["href"].Value);

                    response = WebClient.DownloadString(url);

                    if (!string.IsNullOrEmpty(response))
                    {
                        pattern = "<!--\\ startprint\\ -->(?<lyrics>.+?)<!--\\ stopprint\\ -->";

                        m = Regex.Match(response, pattern, RegexOptions.IgnoreCase);

                        if (m.Success)
                        {
                            string lyrics = m.Groups["lyrics"].Value;

                            lyrics = lyrics.Replace("<br>", "\n");
                            lyrics = lyrics.Trim(" \r\n\t".ToCharArray());

                            lyrics = lyrics.Replace("Â", ""); // Encoding is bit screwed on WowLyrics

                            return lyrics;
                        }
                    }
                }
            }

            return null;
        }

        /// <summary>
        /// Grabs the lyric based on the current <see cref="Context"/> from the webservice.
        /// </summary>
        /// <param name="context"></param>
        /// <returns>
        /// The lyric if one was found, otherwise <c>null</c>.
        /// </returns>
        protected override string GrabLyric(LyricGrabberContext context)
        {
            string url = FormatRequestAddress(context);
            string response = WebClient.DownloadString(url);
            string lyrics = ParseResponse(context, response);

            return lyrics;
        }
    }
}
